//
//  FriendMessageTableViewCell.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/3/31.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "FriendMessageTableViewCell.h"
#import "NetStatusHandle.h"

@interface FriendMessageTableViewCell ()

@property (nonatomic, strong) NSMutableArray *myMarkArray; // 标签数组

@end

@implementation FriendMessageTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

// 自定义初始化方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier markArray:(NSMutableArray *)markArray
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
     //   self.myMarkArray = [NSMutableArray arrayWithArray:markArray];
        [self addMySubview];
    }
    return self;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self addMySubview];
    }
    return self;
}

// 构建视图
- (void)addMySubview
{
    // 用户头像
    self.friendImageV = [[UIImageView alloc] initWithFrame:CGRectMake(16 * kAdjustParamater, 18 * kAdjustParamater, 40 * kAdjustParamater, 40 * kAdjustParamater)];
    self.friendImageV.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.1];
    self.friendImageV.layer.cornerRadius = self.friendImageV.width / 2;
    self.friendImageV.layer.masksToBounds = YES;
    [self.contentView addSubview:_friendImageV];
    
    // 姓名
    self.xingMingLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.friendImageV.right + 7 * kAdjustParamater, 14 * kAdjustParamater, 25 * kAdjustParamater, 13 * kAdjustParamater)];
  //  self.xingMingLabel.backgroundColor = [UIColor redColor];
    self.xingMingLabel.text = @"姓名";
    self.xingMingLabel.font = [UIFont systemFontOfSize:12 * kAdjustParamater];
    [self.contentView addSubview:_xingMingLabel];
    
    // 名字
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.xingMingLabel.right + 6 * kAdjustParamater, self.xingMingLabel.top, 50 * kAdjustParamater, 13 * kAdjustParamater)];
 //   self.nameLabel.backgroundColor = [UIColor redColor];
    self.nameLabel.text = @"我叫盖伦盖伦123";
//    [self.nameLabel sizeToFit];
//    self.nameLabel.frame = CGRectMake(self.nameLabel.frame.origin.x, self.nameLabel.frame.origin.y, self.nameLabel.frame.size.width, 11 * kAdjustParamater);
    self.nameLabel.font = [UIFont systemFontOfSize:12 * kAdjustParamater];
    [self.contentView addSubview:_nameLabel];
    
    // 性别图片
    self.genderImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.nameLabel.right + 8 * kAdjustParamater, self.nameLabel.top + 1 * kAdjustParamater, 13 * kAdjustParamater, 13 * kAdjustParamater)];
//    self.genderImageView.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:_genderImageView];
    
    
//    // 标签
//    for (int i = 0; i < self.myMarkArray.count; i++) {
//
//        // 进行标签的动态布局
//        UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.xingMingLabel.left + tempX + 5 * kAdjustParamater, self.xingMingLabel.bottom + 4 * kAdjustParamater, 50, 19 * kAdjustParamater)];
//        // 取出内容
//        tempLabel.text = self.myMarkArray[i];
//        [tempLabel sizeToFit];
//        tempLabel.frame = CGRectMake(tempLabel.frame.origin.x, tempLabel.frame.origin.y, tempLabel.frame.size.width, 19 * kAdjustParamater);
//        tempX = tempLabel.frame.size.width;
//        // 添加边框
//        [tempLabel.layer setBorderWidth:1];
//        tempLabel.layer.borderColor = [[UIColor colorWithRed:84 / 255.0 green:199 / 255.0 blue:222 / 255.0 alpha:1] CGColor];
//        [self.contentView addSubview:tempLabel];
//    }
    
//    // 账号
//    self.zhaoHaoLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.xingMingLabel.left, self.xingMingLabel.bottom + 30 * kAdjustParamater, 30 * kAdjustParamater, 11 * kAdjustParamater)];
// //   self.zhaoHaoLabel.backgroundColor = [UIColor redColor];
//    self.zhaoHaoLabel.text = @"账号:";
//    self.zhaoHaoLabel.font = [UIFont systemFontOfSize:12 * kAdjustParamater];
//    self.zhaoHaoLabel.textColor = [UIColor colorWithRed:84 / 255.0 green:199 / 255.0 blue:222 / 255.0 alpha:1];
//    [self.contentView addSubview:_zhaoHaoLabel];
//    
//    // ID
//    self.IDLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.zhaoHaoLabel.right + 6 * kAdjustParamater, self.zhaoHaoLabel.top, 100 * kAdjustParamater, 11 * kAdjustParamater)];
//   // self.IDLabel.backgroundColor = [UIColor redColor];
//    self.IDLabel.font = [UIFont systemFontOfSize:12 * kAdjustParamater];
//    self.IDLabel.textColor = [UIColor colorWithRed:84 / 255.0 green:199 / 255.0 blue:222 / 255.0 alpha:1];
//    self.IDLabel.textAlignment = NSTextAlignmentLeft;
//    [self.contentView addSubview:_IDLabel];
    
    // 基因相似度
    UILabel *geneXiangSuDu = [[UILabel alloc] initWithFrame:CGRectMake(self.xingMingLabel.left, self.xingMingLabel.bottom + 30 * kAdjustParamater, 65 * kAdjustParamater, 11 * kAdjustParamater)];
   // geneXiangSuDu.backgroundColor = [UIColor redColor];
    geneXiangSuDu.text = @"个性签名:";
    geneXiangSuDu.textAlignment = NSTextAlignmentLeft;
    geneXiangSuDu.font = [UIFont systemFontOfSize:12 * kAdjustParamater];
    geneXiangSuDu.textColor = kCyanColor;
    [self.contentView addSubview:geneXiangSuDu];
    
    CGRect newFrameOfGeneXiangSiDu = geneXiangSuDu.frame;
    newFrameOfGeneXiangSiDu.size.width = [[NetStatusHandle shareHandle] adjustWidth:geneXiangSuDu.text size:12 height:11];
    geneXiangSuDu.frame = newFrameOfGeneXiangSiDu;
    
  //  NSLog(@"个性签名标题top: %f", geneXiangSuDu.top);
    
    // 基因相似度label
    self.geneSimilarityLabel = [[UILabel alloc] initWithFrame:CGRectMake(geneXiangSuDu.right + 6 * kAdjustParamater, geneXiangSuDu.top, 250 * kAdjustParamater, 11 * kAdjustParamater)];
  //  self.geneSimilarityLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    self.geneSimilarityLabel.font = [UIFont systemFontOfSize:k12Big];
    self.geneSimilarityLabel.textColor = kCyanColor;
   // self.geneSimilarityLabel.textAlignment = NSTextAlignmentLeft;
    self.geneSimilarityLabel.numberOfLines = 0;
    [self.contentView addSubview:_geneSimilarityLabel];
    
    // 关注
    self.attentionButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.attentionButton.backgroundColor = [UIColor whiteColor];
    self.attentionButton.frame = CGRectMake(220 * kAdjustParamater, 30 * kAdjustParamater, 70 * kAdjustParamater, 25 * kAdjustParamater);
//    [self.attentionButton setTitle:@"+关注" forState:UIControlStateNormal];
//    [self.attentionButton setTitleColor:[UIColor colorWithRed:222 / 255.0 green:138 / 255.0 blue:30 / 255.0 alpha:1] forState:UIControlStateNormal];
    [self.attentionButton.layer setBorderWidth:1];
    self.attentionButton.titleLabel.font = [UIFont systemFontOfSize:14 * kAdjustParamater];
    self.attentionButton.layer.cornerRadius = 3 * kAdjustParamater;
    self.attentionButton.layer.borderColor =[[[UIColor blackColor] colorWithAlphaComponent:0.3] CGColor];
    [self.contentView addSubview:_attentionButton];
    
    // 好友
    self.addFriendButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.addFriendButton.backgroundColor = [UIColor whiteColor];
    self.addFriendButton.frame = CGRectMake(self.attentionButton.right + 10 * kAdjustParamater, self.attentionButton.top, 70 * kAdjustParamater, 25 * kAdjustParamater);
//    [self.addFriendButton setTitle:@"+好友" forState:UIControlStateNormal];
//    [self.addFriendButton setTitleColor:[UIColor colorWithRed:222 / 255.0 green:138 / 255.0 blue:30 / 255.0 alpha:1] forState:UIControlStateNormal];
    [self.addFriendButton.layer setBorderWidth:1];
    self.addFriendButton.titleLabel.font = [UIFont systemFontOfSize:14 * kAdjustParamater];
    self.addFriendButton.layer.cornerRadius = 3 * kAdjustParamater;
    self.addFriendButton.layer.borderColor = [[[UIColor blackColor] colorWithAlphaComponent:0.3] CGColor];
    [self.contentView addSubview:_addFriendButton];
    
    
}

- (void)setModel:(CeShiModelOfFriendMessage *)model
{
    // 好友标签
    self.myMarkArray = [NSMutableArray arrayWithArray: [model valueForKey:@"mark"]];
    if (self.myMarkArray.count < 2) {
        CGFloat tempX = 0.0;
        // 标签
        for (int i = 0; i < self.myMarkArray.count; i++) {
            
            // 进行标签的动态布局
            UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.xingMingLabel.left + tempX , self.xingMingLabel.bottom + 4 * kAdjustParamater, 50, 19 * kAdjustParamater)];
            tempLabel.font = [UIFont systemFontOfSize:11 * kAdjustParamater];
            // 取出内容
            tempLabel.text = self.myMarkArray[i];
            tempLabel.textColor = [UIColor colorWithRed:84 / 255.0 green:199 / 255.0 blue:222 / 255.0 alpha:1];
            tempLabel.textAlignment = NSTextAlignmentCenter;
            CGRect tempRect = tempLabel.frame;
            tempRect.size.width   = [[NetStatusHandle shareHandle] adjustWidth:tempLabel.text size:11 height:19] + 10 * kAdjustParamater;
            tempLabel.frame = tempRect;
            tempX = tempLabel.frame.size.width + 5 * kAdjustParamater + tempX;
            // 添加边框
            [tempLabel.layer setBorderWidth:1];
            tempLabel.layer.borderColor = [[UIColor colorWithRed:84 / 255.0 green:199 / 255.0 blue:222 / 255.0 alpha:1] CGColor];
            [self.contentView addSubview:tempLabel];
        }

    } else {
        CGFloat tempX = 0.0;
        // 标签
        for (int i = 0; i < 2; i++) {
            
            // 进行标签的动态布局
            UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.xingMingLabel.left + tempX , self.xingMingLabel.bottom + 4 * kAdjustParamater, 50, 19 * kAdjustParamater)];
            tempLabel.font = [UIFont systemFontOfSize:11 * kAdjustParamater];
            // 取出内容
            tempLabel.text = self.myMarkArray[i];
            tempLabel.textColor = [UIColor colorWithRed:84 / 255.0 green:199 / 255.0 blue:222 / 255.0 alpha:1];
            tempLabel.textAlignment = NSTextAlignmentCenter;
            CGRect tempRect = tempLabel.frame;
            tempRect.size.width   = [[NetStatusHandle shareHandle] adjustWidth:tempLabel.text size:11 height:19] + 10 * kAdjustParamater;
            tempLabel.frame = tempRect;
            tempX = tempLabel.frame.size.width + 5 * kAdjustParamater + tempX;
            // 添加边框
            [tempLabel.layer setBorderWidth:1];
            tempLabel.layer.borderColor = [[UIColor colorWithRed:84 / 255.0 green:199 / 255.0 blue:222 / 255.0 alpha:1] CGColor];
            [self.contentView addSubview:tempLabel];
        }

    }
       // 名字
    self.nameLabel.text = model.name;
    CGRect newRect = self.nameLabel.frame;
    newRect.size.width = [[NetStatusHandle shareHandle] adjustWidth:self.nameLabel.text size:12 height:11];
    self.nameLabel.frame = newRect;
       // 性别图标
        self.genderImageView.frame = CGRectMake(self.nameLabel.right + 8 * kAdjustParamater, self.genderImageView.frame.origin.y, self.genderImageView.frame.size.width, self.genderImageView.frame.size.height);
    // ID
    self.IDLabel.text = model.myID;
    
    // 性别
    if ([model.gender isEqualToString:@"男"]) {
        self.genderImageView.image = [UIImage imageNamed:@"nan-shilibaogao"];
    } else {
        self.genderImageView.image = [UIImage imageNamed:@"nv-shilibaogao"];
    }
   
    // 好友头像
    self.friendImageV.image = [UIImage imageNamed:model.friendImage];
 
#warning 如果没有，添加临时的
    if (model.isHaveGuanZhu.length == 0) {
        model.isHaveGuanZhu = @"NO";
    }
    if (model.isHaveTianjia.length == 0) {
        model.isHaveTianjia = @"NO";
    }

    // 关注或者是没有关注
    if ([model.isHaveGuanZhu isEqualToString:@"YES"]) {
        // 应该取消关注按钮
       // self.attentionButton.userInteractionEnabled = NO;
        [self.attentionButton setTitle:@"已关注" forState:UIControlStateNormal];
        [self.attentionButton setTitleColor:[[UIColor blackColor] colorWithAlphaComponent:0.5] forState:UIControlStateNormal];
    } else if ([model.isHaveGuanZhu isEqualToString:@"NO"]) {
        // 关注按钮开启
       // self.attentionButton.userInteractionEnabled = YES;
        [self.attentionButton setTitle:@"+关注" forState:UIControlStateNormal];
        [self.attentionButton setTitleColor:[UIColor colorWithRed:222 / 255.0 green:138 / 255.0 blue:30 / 255.0 alpha:1] forState:UIControlStateNormal];
    }
    
    

    // 添加或者是没有添加
    if ([model.isHaveTianjia isEqualToString:@"YES"]) {
        // 应该取消添加按钮
      //  self.addFriendButton.userInteractionEnabled = NO;
        [self.addFriendButton setTitle:@"已添加" forState:UIControlStateNormal];
        [self.addFriendButton setTitleColor:[[UIColor blackColor] colorWithAlphaComponent:0.5] forState:UIControlStateNormal];
    } else if ([model.isHaveTianjia isEqualToString:@"NO"]) {
        // 添加按钮开启
      //  self.addFriendButton.userInteractionEnabled = YES;
        [self.addFriendButton setTitle:@"+好友" forState:UIControlStateNormal];
        [self.addFriendButton setTitleColor:[UIColor colorWithRed:222 / 255.0 green:138 / 255.0 blue:30 / 255.0 alpha:1] forState:UIControlStateNormal];
    }
#warning 随机数
    NSString *baifeihao = @"%";
    self.geneSimilarityLabel.text = [NSString stringWithFormat:@"%u%@",arc4random() % 101, baifeihao];
}

- (void)setZgwSearchFriendModel:(ZGWSearchFriendModel *)zgwSearchFriendModel
{
    // 好友标签
    self.myMarkArray = [NSMutableArray array];
    NSArray *tagsArray = zgwSearchFriendModel.tags;
    for (NSDictionary *tagDic in tagsArray) {
        [self.myMarkArray addObject:[tagDic valueForKey:@"name"]];
    }
    if (self.myMarkArray.count < 2) {
        CGFloat tempX = 0.0;
        // 标签
        for (int i = 0; i < self.myMarkArray.count; i++) {
            
            // 进行标签的动态布局
            UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.xingMingLabel.left + tempX , self.xingMingLabel.bottom + 4 * kAdjustParamater, 50 * kAdjustParamater, 19 * kAdjustParamater)];
            tempLabel.font = [UIFont systemFontOfSize:11 * kAdjustParamater];
            // 取出内容
            tempLabel.text = self.myMarkArray[i];
            tempLabel.textColor = [UIColor colorWithRed:84 / 255.0 green:199 / 255.0 blue:222 / 255.0 alpha:1];
            tempLabel.textAlignment = NSTextAlignmentCenter;
            CGRect tempRect = tempLabel.frame;
            tempRect.size.width   = [[NetStatusHandle shareHandle] adjustWidth:tempLabel.text size:11 height:19] + 10 * kAdjustParamater;
            tempLabel.frame = tempRect;
            tempX = tempLabel.frame.size.width + 5 * kAdjustParamater + tempX;
            // 添加边框
            [tempLabel.layer setBorderWidth:1];
            tempLabel.layer.borderColor = [[UIColor colorWithRed:84 / 255.0 green:199 / 255.0 blue:222 / 255.0 alpha:1] CGColor];
            [self.contentView addSubview:tempLabel];
        }
        
    } else {
        CGFloat tempX = 0.0;
        // 标签
        for (int i = 0; i < self.myMarkArray.count; i++) {
            // 首先计算一下下一个的宽度
            CGFloat nextWidth = [[NetStatusHandle shareHandle] adjustWidth:self.myMarkArray[i] size:11 height:19] + 10 * kAdjustParamater;
            if ((tempX + nextWidth + self.xingMingLabel.left + 5) >= 200 * kAdjustParamater) {
                // 说明下一次将超过关注
            } else {
                // 进行标签的动态布局
                UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.xingMingLabel.left + tempX , self.xingMingLabel.bottom + 4 * kAdjustParamater, 50 * kAdjustParamater, 19 * kAdjustParamater)];
                tempLabel.font = [UIFont systemFontOfSize:11 * kAdjustParamater];
                // 取出内容
                tempLabel.text = self.myMarkArray[i];
                tempLabel.textColor = [UIColor colorWithRed:84 / 255.0 green:199 / 255.0 blue:222 / 255.0 alpha:1];
                tempLabel.textAlignment = NSTextAlignmentCenter;
                CGRect tempRect = tempLabel.frame;
                tempRect.size.width   = [[NetStatusHandle shareHandle] adjustWidth:tempLabel.text size:11 height:19] + 10 * kAdjustParamater;
                tempLabel.frame = tempRect;
                tempX = tempLabel.frame.size.width + 5 * kAdjustParamater + tempX;
                // 添加边框
                [tempLabel.layer setBorderWidth:1];
                tempLabel.layer.borderColor = [[UIColor colorWithRed:84 / 255.0 green:199 / 255.0 blue:222 / 255.0 alpha:1] CGColor];
                [self.contentView addSubview:tempLabel];
            }

            }
            
            
    }
    
    // 名字
    self.nameLabel.text = zgwSearchFriendModel.nikename;
    CGRect newRect = self.nameLabel.frame;
    newRect.size.width = [[NetStatusHandle shareHandle] adjustWidth:self.nameLabel.text size:12 height:13];
    self.nameLabel.frame = newRect;
    
    // 性别图标
    self.genderImageView.frame = CGRectMake(self.nameLabel.right + 8 * kAdjustParamater, self.genderImageView.frame.origin.y, self.genderImageView.frame.size.width, self.genderImageView.frame.size.height);
    // 性别
    if ([zgwSearchFriendModel.sex isEqualToString:@"0"]) {
        // 男
        self.genderImageView.image = [UIImage imageNamed:@"nan-shilibaogao"];
    } else {
        self.genderImageView.image = [UIImage imageNamed:@"nv-shilibaogao"];
    }
    
    [self.friendImageV sd_setImageWithURL:[NSURL URLWithString:zgwSearchFriendModel.avatar_url] placeholderImage:[UIImage imageNamed:@""]];
    
    // 关注或者是没有关注
    NSString *isAttention = [NSString stringWithFormat:@"%@", zgwSearchFriendModel.is_attention];
    if ([isAttention isEqualToString:@"1"]) {
        // 应该取消关注按钮
        //   self.attentionButton.userInteractionEnabled = NO;
        [self.attentionButton setTitle:@"已关注" forState:UIControlStateNormal];
        [self.attentionButton setTitleColor:[[UIColor blackColor] colorWithAlphaComponent:0.5] forState:UIControlStateNormal];
    } else if ([isAttention isEqualToString:@"0"]) {
        // 关注按钮开启
        //    self.attentionButton.userInteractionEnabled = YES;
        [self.attentionButton setTitle:@"+关注" forState:UIControlStateNormal];
        [self.attentionButton setTitleColor:[UIColor colorWithRed:222 / 255.0 green:138 / 255.0 blue:30 / 255.0 alpha:1] forState:UIControlStateNormal];
    }
    
    
    
    // 添加或者是没有添加
    NSString *isFriend = [NSString stringWithFormat:@"%@", zgwSearchFriendModel.is_friend];
    if ([isFriend isEqualToString:@"1"]) {
        // 应该取消添加按钮
        //     self.addFriendButton.userInteractionEnabled = NO;
        [self.addFriendButton setTitle:@"已添加" forState:UIControlStateNormal];
        [self.addFriendButton setTitleColor:[[UIColor blackColor] colorWithAlphaComponent:0.5] forState:UIControlStateNormal];
    } else if ([isFriend isEqualToString:@"0"]) {
        // 添加按钮开启
        //    self.addFriendButton.userInteractionEnabled = YES;
        [self.addFriendButton setTitle:@"+好友" forState:UIControlStateNormal];
        [self.addFriendButton setTitleColor:[UIColor colorWithRed:222 / 255.0 green:138 / 255.0 blue:30 / 255.0 alpha:1] forState:UIControlStateNormal];
    }
    NSString *geXingQianMingLabel = [NSString stringWithFormat:@"%@", zgwSearchFriendModel.signatures];
    //  NSLog(@"个性签名：%@", geXingQianMingLabel);
    if (geXingQianMingLabel.length == 0 || [geXingQianMingLabel isEqualToString:@"<null>"] || [geXingQianMingLabel isEqualToString:@"(null)"]) {
        self.geneSimilarityLabel.text = @"暂时没有个性签名";
    } else {
        self.geneSimilarityLabel.text = zgwSearchFriendModel.signatures;
    }
    
    // 个性签名自适应高度
    CGRect newFrameOfGeneSimilarityLabel = self.geneSimilarityLabel.frame;
    newFrameOfGeneSimilarityLabel.size.height = [[NetStatusHandle shareHandle] adjustHeight:self.geneSimilarityLabel.text size:12 width:240];
    self.geneSimilarityLabel.frame = CGRectMake(newFrameOfGeneSimilarityLabel.origin.x, newFrameOfGeneSimilarityLabel.origin.y - 0.5 * kAdjustParamater, newFrameOfGeneSimilarityLabel.size.width, newFrameOfGeneSimilarityLabel.size.height);
   // NSLog(@"个性签名top：%f", self.geneSimilarityLabel.top);
    

}


//- (void)setNameLabel:(UILabel *)nameLabel
//{
//    self.nameLabel.text = nameLabel.text;
//    [self.nameLabel sizeToFit];
//    self.nameLabel.frame = CGRectMake(self.nameLabel.frame.origin.x, self.nameLabel.frame.origin.y, self.nameLabel.frame.size.width, 11 * kAdjustParamater);
//    // 性别图标
//    self.genderImageView.frame = CGRectMake(self.nameLabel.right + 8 * kAdjustParamater, self.genderImageView.frame.origin.y, self.genderImageView.frame.size.width, self.genderImageView.frame.size.height);
//}

@end
